package com.kgc.controller;

import com.kgc.basic.pojo.Provider;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.feign.ImportExportFeign;
import com.kgc.feign.ProviderFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.Map;

@RestController
@RequestMapping("/Provider")
public class ProviderController {

    private Logger logger = LoggerFactory.getLogger(ProviderController.class);

    @Autowired
    private ProviderFeign providerFeign;

    @Autowired
    private ImportExportFeign importExportFeign;

    @RequestMapping("selectAllSupplier")
    public ResponseMsg selectAllSupplier() {
        logger.info("ProviderController selectAllSupplier is start...");
        return providerFeign.selectAllSupplier();
    }

    @RequestMapping("/selectProviderByPage")
    public ResponseMsg selectProviderByPage(@RequestBody Map<String, Object> map) {
        logger.info("ProviderController   selectProviderByPage params {}", map);
        return providerFeign.selectProviderByPage(map);
    }

    @RequestMapping("/insertProvider")
    public ResponseMsg insertProvider(@RequestBody Provider Provider) {
        logger.info("ProviderController  insertProvider params {}", Provider);
        return providerFeign.insertProvider(Provider);
    }

    @RequestMapping("/updateProvider")
    public ResponseMsg updateProvider(@RequestBody Provider Provider) {
        logger.info("ProviderController  updateProvider params {}", Provider);
        return providerFeign.updateProvider(Provider);
    }

    @RequestMapping("/deleteProvider")
    public ResponseMsg deleteProvider(@RequestParam("id") String id) {
        logger.info("ProviderController  deleteProvider params {}", id);
        return providerFeign.deleteProvider(Integer.parseInt(id));
    }

    @RequestMapping("selectProviderByCode")
    public ResponseMsg selectProviderByCode(@RequestParam("code") String code) {
        logger.info("consumer ProviderController selectProviderByCode code:{}", code);
        return providerFeign.selectCustomerByCode(code);
    }

    @RequestMapping(value = "/import", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseMsg importInfo(@RequestPart("file") MultipartFile file) {
        logger.info("consumer CustomerController importInfo fileName:{}", file.getOriginalFilename());
        return importExportFeign.importInfo(file, "","code", "providerServiceImpl");
    }

    @RequestMapping(value = "/template", method = RequestMethod.GET)
    public void downTemplate(HttpServletResponse response) {
        logger.info("consumer CustomerController downTemplate is start...");
        try {
            ResponseMsg responseMsg = importExportFeign.downLoadTemplate("com.kgc.basic.pojo.Provider");
            if ("200".equals(responseMsg.getCode())) {
                String base64Data = (String) responseMsg.getData();
                byte[] templateData = Base64.getDecoder().decode(base64Data);   //解码

                //设置响应头
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                response.setCharacterEncoding("UTF-8");
                String filename = URLEncoder.encode("供应商信息表.xlsx", "UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);

                response.getOutputStream().write(templateData);
            } else {
                logger.error("下载模板失败, status: {}", responseMsg.getMsg());
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        } catch (IOException e) {
            logger.error("consumer CustomerController downTemplate is error", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "/export")
    public void exportCustomers(@RequestBody Map<String, Object> map, HttpServletResponse response) {
        logger.info("consumer CustomerController exportCustomers params:{}", map);
        try {
            ResponseMsg responseMsg = importExportFeign.exportEntities(map, "com.kgc.basic.pojo.Provider", "createDate desc");
            if ("200".equals(responseMsg.getCode())) {
                String base64Data = (String) responseMsg.getData();
                byte[] exportData = Base64.getDecoder().decode(base64Data);

                //设置响应头
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                response.setCharacterEncoding("UTF-8");
                String filename = URLEncoder.encode("供应商信息表.xlsx", "UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);

                response.getOutputStream().write(exportData);
            } else {
                logger.error("导出失败, msg: {}", responseMsg.getMsg());
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        } catch (IOException e) {
            logger.error("consumer CustomerController exportCustomers is error", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getProviderAllByData")
    public ResponseMsg getProviderAllByData(){
        logger.info("consumer getProviderAllByData is start...");
        return providerFeign.getProviderAllByData();
    }

}
